/**
 * Created by 蔡岳衡 on 2016/7/27.
 */

var dbConnInfo = function () {

    //版本号
    var version = 1;

    //模块url
    var url = path + '/action/powerdb/designer/dbConnInfo/';

    //获取的url
    var getUrl = '';

    //删除的url
    var delUrl = '';

    //数据库查找表单选择器
    var SearchFromSeletor = '#dbSearchForm';

    //数据库表格选择器
    var dbJqTableSeletor = '#dbInfoJqgridTable';

    var dbJqPagerSeletor = '#dbInfoJqgridPager';

    //数据库关联账户表格选择器
    var accJqTableSelector = '#accReDbJqgridTable';

    var accJqPagerSelector = '#accReDbJqPagerTable';

    //数据库新增或修改弹出框
    var dbOpenWinSelector = '#dbAddOrEditModal';

    //数据库关联账户新增弹出框
    var accOpenWinSelector = '#accAddModal';

    //数据库新增或修改弹出框内表单选择器
    var dbAddOrEditModelFormSeletor = '#dbAddOrEditModelForm';

    //用户管理弹出框中账户列表
    var accJqAddTableSelector = '#accAddJqgridTable';
    var accJqAddPagerSelector = '#accAddJqgridPager';

    //关联用户弹出框内表单选择器
    var accAddModalForm = '#accAddTableForm';

    //修改密码弹出框内表单选择器
    var modifyPasswordModalForm = '#motifyPasswordForm';

    var id;

    var resData;

    //连接字符串
    var DBBOX_URL = '';
    var ORACLE_URL = 'jdbc:oracle:thin:@ip:port:dbName';
    var MYSQL_URL = 'jdbc:mysql://ip:port/dbName?useUnicode=true&characterEncoding=encoding';

    var DBBOX_URL_REPLACE = DBBOX_URL;
    var ORACLE_URL_REPLACE = ORACLE_URL;
    var MYSQL_URL_REPLACE = MYSQL_URL;

    /** 枚举关键字定义 */
    var enums = ['pd_db_conn_info-status', 'pd_db_conn_info-type', 'pd_db_conn_info-charset', 'sec_account-sex'];

    /** 获取所有需要翻译的枚举 */
    var enumMap = sPost.getEnumMap(enums);

    var accGridSetting = {
        gridId: accJqTableSelector,
        pagerId: accJqPagerSelector,
        datatype: 'local',
        data: [],
        //height: 150,
        gridHeightType: 2,
        url: null,
        mtype: "post",
        multiselect: true,
        rownumbers: true,
        autowidth: true,
        altRows: true,
        rowNum: 5,
        rowList: [5, 15, 30],
        colNames: ['id', '用户名称'],
        colModel: [
            {name: 'id', index: 'id', width: 0, align: 'center', hidden: true},
            {name: 'nameCn', index: 'nameCn', width: 0, align: 'center'}
        ]
    };

    var dbConnInfoGridSetting = {
        gridId: dbJqTableSeletor,
        pagerId: dbJqPagerSeletor,
        datatype: 'local',
        data: [],
        //height: 140,
        gridHeightType: 2,
        url: null,
        mtype: "post",
        multiselect: false,
        rownumbers: true,
        autowidth: true,
        rowNum: 5,
        onSelectRow: function (id) {
            dbConnInfo.searchDbConnInfoRelAccount(id);
        },
        rowList: [5, 15, 30],
        colNames: ['id', '连接名', '数据库名', '存储端口', '存储状态', '存储类型', '编码', 'ip地址',
            '创建人', '修改人', '创建时间', '修改时间', '描述', 'url', '创建人id',
            '修改人id'],
        colModel: [
            {name: 'id', index: 'id', width: 0, align: 'center', hidden: true},
            {name: 'name', index: 'name', width: 0, align: 'center'},
            {name: 'dbName', index: 'dbName', width: 0, align: 'center'},
            {name: 'port', index: 'port', width: 0, align: 'center'},
            {name: 'status', index: 'status', width: 0, align: 'center'},
            {name: 'type', index: 'type', width: 0, align: 'center'},
            {name: 'charset', index: 'charset', width: 0, align: 'center'},
            {name: 'ip', index: 'ip', width: 0, align: 'center'},
            {name: 'createAccount', index: 'createAccount', width: 0, align: 'center'},
            {name: 'modifyAccount', index: 'modifyAccount', width: 0, align: 'center'},
            {name: 'createTime', index: 'createTime', width: 0, align: 'center'},
            {name: 'modifyTime', index: 'modifyTime', width: 0, align: 'center'},
            {name: 'remark', index: 'remark', width: 0, align: 'center'},
            {name: 'url', index: 'url', width: 0, align: 'center', hidden: true},
            {name: 'createAccountId', index: 'createAccountId', width: 0, align: 'center', hidden: true},
            {name: 'modifyAccountId', index: 'modifyAccountId', width: 0, align: 'center', hidden: true}

        ]
    };

    var accJqAddTableSetting = {
        gridId: accJqAddTableSelector,
        pagerId: accJqAddPagerSelector,
        // datatype: 'json',
        datatype: 'local',
        data: [],
        //height: 260,
        gridHeightType: 2,
        url: null,
        mtype: "post",
        multiselect: true,
        rownumbers: true,
        autowidth: true,
        rowNum: 10,
        rowList: [10, 15, 20],
        colNames: ['id', '中文名称', '性别', '手机', '电话'],
        colModel: [
            {name: 'id', index: 'id', width: 0, align: 'center', hidden: true},
            {name: 'nameCn', index: 'nameCn', width: 0, align: 'center'},
            {name: 'sex', index: 'sex', width: 0, align: 'center'},
            {name: 'cellPhone', index: 'cellPhone', width: 0, align: 'center'},
            {name: 'phone', index: 'phone', width: 0, align: 'center'}
        ]
    };

    /**
     * 获取当前登录用户的id
     */
    function getCurAccountId() {
        return accountId;
    }

    /**
     * 获取翻译反转的枚举值
     * @param enumMap
     * @param key
     * @param value
     * @returns
     */
    function getTrDatas(enumMap, key, value) {
        var arr = enumMap.get(key);
        var result = '';
        $.each(arr, function (i, val) {
            if (value == val.enumValue) {
                result = val.enumCode;
                return false;
            }
        });
        return result;
    }

    $.validator.addMethod("checkIpValid", function (value, element) {

        if (value == 'localhost') {
            return true;
        }

        var exp = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
        var reg = value.match(exp);

        if (reg == null)
            return false;
        else
            return true;
    }, "IP地址不合法");

    $.validator.addMethod("checkPortValid", function (value, element) {
        var exp = /^([0-9]|[1-9]\d|[1-9]\d{2}|[1-9]\d{3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/;
        var reg = value.match(exp);

        if (reg == null)
            return false;
        else
            return true;
    }, "端口号不合法,请输入0到65535的整数");

    /**
     * 根据用户输入值生成url
     */
    function createUrl() {
        var dbName = $('#dbName').val();
        var ip = $('#ip').val();
        var port = $('#port').val();
        var type = $('#addOrEditType').val();
        var encoding = $("#addOrEditCharset").val();

        dbName = sUtils.notNull(dbName) ? dbName : 'dbName';
        ip = sUtils.notNull(ip) ? ip : 'ip';
        port = sUtils.notNull(port) ? port : 'port';
        encoding = encoding == 1 ? 'UTF-8' : 'GBK';

        MYSQL_URL_REPLACE = MYSQL_URL;
        /*ORACLE_URL_REPLACE = ORACLE_URL_REPLACE
         .replace('dbName', dbName)
         .replace('ip', ip)
         .replace('port', port);*/

        MYSQL_URL_REPLACE = MYSQL_URL_REPLACE
            .replace('dbName', dbName)
            .replace('ip', ip)
            .replace('port', port)
            .replace('encoding', encoding);

        //slog.log(ORACLE_URL_REPLACE);
        slog.log(MYSQL_URL_REPLACE);

        if (type == 1) {
            $('#dbUrl').val(MYSQL_URL_REPLACE);
        } else {
            $('#dbUrl').val('null');
        }
    }

    function formChange() {
        //数据源类型和驱动类型联动
        $('#addOrEditType').change(function () {
            createUrl();
        });

        $('#dbName').change(function () {
            createUrl();
        });

        $('#ip').change(function () {
            createUrl();
        });

        $('#port').change(function () {
            createUrl();
        });

        $('#addOrEditCharset').change(function () {
            createUrl();
        });
    }


    return {

        /*------数据库管理模块begin------*/
        /**
         初始化
         1.下拉框加载枚举数据
         2.初始化所有表格
         3.查询所有数据库连接信息，并填充到表格中
         4.绑定按钮
         */
        init: function () {

            function initEnum() {
                sUtils.select2Enum({
                    selector: '#dbStatus',
                    enums: enumMap.get('pd_db_conn_info-status'),
                    addAll: true
                });
                sUtils.select2Enum({
                    selector: '#dbType',
                    enums: enumMap.get('pd_db_conn_info-type'),
                    addAll: true
                });
                sUtils.select2Enum({
                    selector: '#dbCharset',
                    enums: enumMap.get('pd_db_conn_info-charset'),
                    addAll: true
                });
            }

            function initJqGrid(setting) {
                sGrid.jqgridSimple(setting);
                sGrid.jqgridReSize(setting);
                sGrid.jqgridNotButton(setting['gridId'], setting['pagerId']);
                sGrid.jqgridDestory(setting['gridId']);
            }

            initEnum();
            dbConnInfo.searchDbConnInfo(true);
            dbConnInfo.searchDbConnInfo(false);

            sUtils.formValidClass(dbAddOrEditModelFormSeletor, {
                port: 'checkPortValid',
                ip: 'checkIpValid'

            });
            sUtils.formValidClass(modifyPasswordModalForm);

            initJqGrid(accGridSetting);
            initJqGrid(accJqAddTableSetting);
        },

        /**
         * 查询数据库连接信息
         * @param flag
         *  flag:true 首次加载，初始化表格 flag:false 二次加载，查找数据
         */
        searchDbConnInfo: function (flag) {

            if (flag) { //第一次加載

                sGrid.jqgridSimple(dbConnInfoGridSetting);
                sGrid.jqgridReSize(dbConnInfoGridSetting);
                sGrid.jqgridNotButton(dbConnInfoGridSetting['gridId'], dbConnInfoGridSetting['pagerId']);
                sGrid.jqgridDestory(dbConnInfoGridSetting['gridId']);

            } else { // 第二次加載
                /*
                 * 获取查询表单中的参数
                 * 请求action,获取返回数据
                 */

                var params = sUtils.getFormParams(SearchFromSeletor);

                var newUrl = url + "findDBInfo";
                sPost.ajaxFind(newUrl, params, function (data) {
                    if (data.success) {
                        resData = data.obj;

                        sUtils.transactionEnum(resData, enums, enumMap);

                        $(dbJqTableSeletor).clearGridData();
                        if (resData != null) {
                            $(dbJqTableSeletor).setGridParam({data: resData});
                        }
                        $(dbJqTableSeletor).trigger("reloadGrid");
                    } else {
                        slog.error("find Method Error : " + data.msg);
                    }
                });
            }
        },

        /**
         * 查询数据库连接信息关联账户
         * @param id 点击的数据库连接信息表格行Id
         */
        searchDbConnInfoRelAccount: function (id) {
            //单击行
            slog.log(id);
            // 根据id获取选中行数据
            getUrl = url + 'getDbRelAccount/' + id;
            slog.log(getUrl);

            sPost.ajax(getUrl, null, function (data) {
                if (data.success) {
                    slog.log("查询数据库关联账户成功");
                    slog.log(data);

                    var accountData = data.obj;
                    slog.log(accountData);
                    $(accJqTableSelector).clearGridData();
                    $(accJqTableSelector).setGridParam({data: accountData});
                    $(accJqTableSelector).trigger("reloadGrid");
                } else {
                    layer.alert('查询数据库关联账户失败！' + data.msg, {icon: 5, title: '提示'});
                }

            });
        },

        /**
         * 查询账户信息
         */
        searchAccountInfo: function () {
            slog.log("用户查询");

            var params = sUtils.getFormParams(accAddModalForm);
            slog.log(params);

            $.extend(params, {'Q_delStatus_=_short': 0});
            var findAccountUrl = path + '/action/security/account/find';

            sPost.ajaxFind(findAccountUrl, params, function (data) {
                if (data.success) {
                    resData = data.obj;

                    sUtils.transactionEnum(resData.rows, enums, enumMap);

                    $(accJqAddTableSelector).clearGridData();
                    if (resData != null) {
                        $(accJqAddTableSelector).setGridParam({data: resData.rows});
                    }
                    $(accJqAddTableSelector).trigger("reloadGrid");
                } else {
                    layer.alert('新增失败！' + data.msg, {icon: 5, title: '提示'});
                }
            });
        },

        /**
         * 打开增加或修改数据库连接信息弹窗
         * @param flag
         *  true: 新增  false: 修改
         */
        openWinAddOrEditDbConnInfo: function (flag) {

            function initEnum() {
                sUtils.select2Enum({
                    selector: '#addOrEditStatus',
                    enums: enumMap.get('pd_db_conn_info-status')
                });
                sUtils.select2Enum({
                    selector: '#addOrEditType',
                    enums: enumMap.get('pd_db_conn_info-type')
                });
                sUtils.select2Enum({
                    selector: '#addOrEditCharset',
                    enums: enumMap.get('pd_db_conn_info-charset')
                });
            }

            /* 清空弹窗表单  */
            function cleanOpenWin() {
                sUtils.cleanFrom(dbAddOrEditModelFormSeletor);
                sUtils.validResetForm(dbAddOrEditModelFormSeletor);
                //如果有特殊控件，还需要特殊处理；
                $(dbAddOrEditModelFormSeletor).find('#dbUrl').val('');
                $('#password').removeAttr("readonly");
            }

            /*
             加载表单数据
             */
            function initOpenWin(data) {
                sUtils.setFrom(dbAddOrEditModelFormSeletor, data);
                //如果有特殊控件，还需要特殊处理；
                $('#addOrEditStatus').select2("val", getTrDatas(enumMap, 'pd_db_conn_info-status', data.status));
                $('#addOrEditType').select2("val", getTrDatas(enumMap, 'pd_db_conn_info-type', data.type));
                $('#addOrEditCharset').select2("val", getTrDatas(enumMap, 'pd_db_conn_info-charset', data.charset));
                $('#dbUrl').val(data.url);
                $('#remark').val(data.remark);

            }

            formChange();
            cleanOpenWin();
            initEnum();

            if (!flag) { //修改
                /*
                 判断是否选中行
                 获取修改的数据，填充打表单里
                 */
                // 获取选中行id
                id = $(dbJqTableSeletor).jqGrid('getGridParam', 'selrow');

                // 判断是否选中
                if (sUtils.isNull(id)) {
                    layer.alert('请选择要修改的信息！', {icon: 5, title: '提示'});
                    return;
                }
                $('#editNeedHide').hide();
                resData = $(dbJqTableSeletor).jqGrid('getRowData', id);
                initOpenWin(resData);
            } else {
                $('#editNeedHide').show();
            }
            var title = flag ? '新增' : '修改';
            // layer弹窗按钮
            var at = layer.open({
                type: 1,
                title: title,
                shadeClose: true,
                //shade: 0.8,
                area: ['600px', '44s0px'],
                content: $(dbOpenWinSelector),
                zIndex: 1000,
                btn: ['保存', '取消'],
                yes: function () {
                    var result = dbConnInfo.saveOrEditDbConnInfo(flag);
                    if (result == true) {
                        layer.close(at);
                    }
                }
            });
        },

        /**
         * 增加或修改数据库连接信息
         * @param flag
         *  true: 新增  false: 修改
         */
        saveOrEditDbConnInfo: function (flag) {

            var result = false;
            /*
             获取表单数据
             */
            var params = sUtils.getFormParams(dbAddOrEditModelFormSeletor);
            if ($(dbAddOrEditModelFormSeletor).valid()) {
                result = true;
                if (flag) {
                    params.password = CryptoUtils.aesEncryptUtf8(params.password);
                    params.createAccountId = getCurAccountId();
                    params.modifyAccountId = getCurAccountId();

                    sPost.ajax(url, params, function (data) {
                        if (data.success) {
                            dbConnInfo.searchDbConnInfo(false);
                            layer.alert('新增成功！', {icon: 1, title: '提示'});
                        } else {
                            layer.alert('新增失败！' + data.msg, {icon: 5, title: '提示'});
                            result = false;
                        }
                    });

                } else {
                    params.modifyAccountId = getCurAccountId();
                    sPost.ajaxPut(url, params, function (data) {
                        if (data.success) {
                            dbConnInfo.searchDbConnInfo(false);
                            layer.alert('修改成功！', {icon: 1, title: '提示'});
                        } else {
                            layer.alert('修改失败！' + data.msg, {icon: 5, title: '提示'});
                            result = false;
                        }
                    });
                }
            }
            return result;
        },

        /**
         * 删除数据库连接信息
         */
        delDbConnInfo: function () {
            /*
             判断是否选中
             二次确认
             */
            // 获取选中行id
            id = $(dbJqTableSeletor).jqGrid('getGridParam', 'selrow');
            // 根据id获取选中行数据
            resData = $(dbJqTableSeletor).jqGrid('getRowData', id);
            // 判断是否选中
            if (sUtils.isNull(id)) {
                layer.alert('请选择要删除的用户！', {icon: 5, title: '提示'});
                return;
            }
            if ($(accJqTableSelector).getGridParam("reccount") != 0) {
                layer.alert('请先删除该数据库的用户！', {icon: 5, title: '提示'});
                return;
            }
            layer.confirm('确定要删除数据库' + resData['dbName'] + '吗？', {icon: 3, title: '提示'}, function (index) {

                delUrl = url + resData.id;

                sPost.ajaxDel(delUrl, {}, function (data) {
                    if (data.success) {
                        $(dbJqTableSeletor).jqGrid('delRowData', id);
                        layer.alert('删除成功！', {icon: 1, title: '提示'});
                    } else {
                        layer.alert('删除失败！' + data.msg, {icon: 5, title: '提示'});
                    }
                });

                layer.close(index);
            });
        },

        /*
         * 清空数据库查询条件
         */
        clearDbConnInfoSearchForm: function () {
            sUtils.cleanFrom(SearchFromSeletor);
            //$('#dbStatus').val('');
            //$('#dbType').val('');
            //$('#dbCharset').val('');
            $('#dbStatus').select2('val', '');
            $('#dbType').select2('val', '');
            $('#dbCharset').select2('val', '');
            dbConnInfo.searchDbConnInfo(false);
        },

        /**
         * 打开修改密码弹窗
         */
        openWinModifyPassword: function () {

            /* 清空弹窗表单  */
            function cleanOpenWin() {
                sUtils.cleanFrom(modifyPasswordModalForm);
                sUtils.validResetForm(modifyPasswordModalForm);
                //如果有特殊控件，还需要特殊处理；
            }

            // 获取选中行id
            id = $(dbJqTableSeletor).jqGrid('getGridParam', 'selrow');

            // 判断是否选中
            if (sUtils.isNull(id)) {
                layer.alert('请选择要修改的信息！', {icon: 5, title: '提示'});
                return;
            }

            cleanOpenWin();

            layer.open({
                type: 1,
                title: '修改密码',
                shadeClose: true,
                //shade: 0.8,
                area: ['300px', '195px'],
                content: $('#motifyPasswordModal'),
                zIndex: 1000,
                btn: ['保存', '取消'],
                yes: function (at, layero) {
                    var params = sUtils.getFormParams(modifyPasswordModalForm);
                    if ($.trim(params['newPassword']) != $.trim(params['confirmPassword'])) {
                        layer.alert('两次密码输入不一致，请重新输入！', {icon: 5, title: '提示'});
                        $('#confirmPassword').val('');
                        return;
                    }
                    if ($(modifyPasswordModalForm).valid()) {
                        //密码加密
                        var newPassword = CryptoUtils.aesEncryptUtf8(params['newPassword']);
                        var editUrl = url + 'updatePassword';
                        sPost.ajax(editUrl, {id: id, password: newPassword}, function (data) {
                            if (data.success) {
                                layer.alert('修改成功！', {icon: 1, title: '提示'});
                            } else {
                                layer.alert('修改失败！', {icon: 5, title: '提示'});
                            }
                        });
                        layer.close(at);
                    }
                }
            });
        },

        /*------数据库管理模块end------*/

        /*------用户-数据库关系管理模块begin------*/

        /**
         * 弹出添加关联账户关系弹窗
         */
        openWinAddRelAccount: function () {

            // 获取选中行id
            id = $(dbJqTableSeletor).jqGrid('getGridParam', 'selrow');

            if (sUtils.isNull(id)) {
                layer.alert('请选定数据库！', {icon: 5, title: '提示'});
                return;
            }

            function cleanOpenWin() {
                sUtils.cleanFrom('#accAddTableForm');
                $('#accAddedJqgridTable option').length = 0;
            }

            function initOpenWin() {
                dbConnInfo.searchAccountInfo();
            }

            cleanOpenWin();
            initOpenWin();

            var at = layer.open({
                type: 1,
                title: '添加关联账户',
                shadeClose: true,
                //shade: 0.8,
                area: ['1100px', '540px'],
                content: $(accOpenWinSelector),
                zIndex: 1000
            });
            $('#saveDbRelAccBtn').unbind('click').click(function () {
                if (-1 != dbConnInfo.saveDbRelAccount()) {
                    layer.close(at);
                }
            });
            $('#removeDbRelAccBtn').unbind('click').click(function () {
                layer.close(at);
            });
        },

        /**
         * 清空用户查询表单
         */
        clearAccountSearchForm: function () {
            sUtils.cleanFrom(accAddModalForm);
            $('#userDelStatus').val(0);
            dbConnInfo.searchAccountInfo();
        },

        /**
         * 添加用户进已选表格
         */
        addAccount: function () {
            var selectedIds = $(accJqAddTableSelector).jqGrid("getGridParam", "selarrrow");
            var tempId;

            if (sUtils.isNull(selectedIds)) {
                layer.alert('请选择要添加的用户！', {icon: 5, title: '提示'});
                return;
            }

            var accAddedArr = $('#accAddedJqgridTable option').map(function () {
                return $(this).val();
            });

            for (var i = 0; i < selectedIds.length; i++) {
                tempId = selectedIds[i];

                //判断下拉框有没有重复的id，如果有就不加进去
                if ($.inArray(tempId, accAddedArr) == -1) {
                    resData = $(accJqAddTableSelector).jqGrid('getRowData', tempId);
                    var option = $("<option>").val(tempId).text(resData.nameCn);
                    $('#accAddedJqgridTable').append(option);
                }
            }
        },

        /**
         *   移除已添加的用户
         */
        removeAddedAccount: function () {
            var $option = $('#accAddedJqgridTable').find("option:selected");
            if ($option.length == 0) {
                layer.alert('请选择要移除的用户！', {icon: 5, title: '提示'});
            } else {
                $option.remove();
            }
        },

        /**
         * 保存添加的关联账户关系
         */
        saveDbRelAccount: function () {

            var options = $('#accAddedJqgridTable option');

            if (options.length == 0) {
                layer.alert('请添加用户！', {icon: 5, title: '提示'});
                return -1;
            }

            var accountIds = options.map(function () {
                return $(this).val();
            }).get().join(",");

            sPost.ajax(url + 'addDbRelAccount', {'id': parseFloat(id), 'ids': accountIds}, function (data) {
                if (data.success) {
                    layer.alert('添加成功！', {icon: 1, title: '提示'});
                    dbConnInfo.searchDbConnInfoRelAccount(id);
                } else {
                    layer.alert('添加失败！', {icon: 5, title: '提示'});
                }
            });
        },

        /**
         *  删除关联账户表格条目
         */
        delDbRelAccount: function () {
            // 获取数据库关联账户表格选中行
            var selectedIds = $(accJqTableSelector).jqGrid("getGridParam", "selarrrow");
            // 获取选中的数据库id
            id = $(dbJqTableSeletor).jqGrid('getGridParam', 'selrow');

            // 判断是否选中
            if (sUtils.isNull(selectedIds)) {
                layer.alert('请选择要删除的用户！', {icon: 5, title: '提示'});
                return;
            }

            if (selectedIds.length == 0) return;

            var accountIds = selectedIds.join(",");

            layer.confirm('确定要删除吗？', {icon: 3, title: '提示'}, function (index) {

                delUrl = url + "delDbRelAccount";

                sPost.ajax(delUrl, {'id': parseFloat(id), 'ids': accountIds}, function (data) {
                    if (data.success) {
                        /*
                         selectedIds个数会动态减少，故每次只删除数组的第一个元素，直到数组个数为0
                         */
                        while (selectedIds.length) {
                            $(accJqTableSelector).jqGrid("delRowData", selectedIds[0]);
                        }
                        layer.alert('删除成功！', {icon: 1, title: '提示'});
                    } else {
                        layer.alert('删除失败！', {icon: 5, title: '提示'});
                    }
                });

                layer.close(index);
            });
        }
        /*------用户-数据库关系管理模块end------*/
    }
}();